package com.example.exam.utils;


import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * @author zhang
 */
@Component
@Log4j2
public class MailUtils {
    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    private JavaMailSender sender;

    public static Boolean isEmailValid(String email) {
        return !email.matches("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$");
    }

    public void sendMail(String to, String subject, String text) {
        SimpleMailMessage sm = new SimpleMailMessage();
        sm.setFrom(from);
        log.info("from:{}", from);
        sm.setTo(to);
        sm.setSubject(subject);
        sm.setText(text);
        sender.send(sm);
    }

}
